package com.lib724.service.impl;

import java.sql.SQLException;

import com.google.inject.Inject;
import com.lib724.dao.ManagerDAO;
import com.lib724.dao.ReaderDAO;
import com.lib724.model.Manager;
import com.lib724.service.UserService;
import com.lib724.web.ModelAndError;

public class UserServiceImpl implements UserService {

	@Inject
	private ManagerDAO managerDao;
	@Inject
	private ReaderDAO readerDao;
	
	/* (non-Javadoc)
	 * @see com.lib724.service.impl.UserService#createManager(com.lib724.model.Manager)
	 */
	public ModelAndError<Manager> createManager(Manager manager) {
		ModelAndError<Manager> modelAndError = new ModelAndError<Manager>();
		try {
			int rows = managerDao.selectByUsername(manager.getUsername());
			if (rows > 0) {
				modelAndError.addError("error", "帐号已存在，请选择其他帐号注册！");
				return modelAndError;
			}
			managerDao.insert(manager);
			modelAndError.setModel(manager);
			modelAndError.addError("success", "注册成功！");
		} catch (SQLException e) {
			modelAndError.addError("errors", "数据库错误！");
		}
		return modelAndError;
	}
	
//	public ModelAndError<Manager> queryManagerByUsername(String username) {
//		ModelAndError<Manager> modelAndError = new ModelAndError<Manager>();
//		Manager manager = null;
//		try {
//			manager = managerDao.selectByUsername(username);
//			modelAndError.setModel(manager);
//			modelAndError.addError("errors", "此帐号已存在,请输入其他帐号注册！");
//		} catch (SQLException e) {
//			modelAndError.addError("errors", "数据库错误！");
//		}
//		return modelAndError;
//	}
}
